/**
* @CopyRight weitaming.com
* @author wuyahui
* @E-mail wuyahui@ispied.com
* @version 1.0.0
* @Date  2014年9月3日 下午5:06:16
*/
package com.tstudy.laoshibang.base;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.apache.http.Header;

import com.google.gson.Gson;
import com.igexin.sdk.PushManager;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.manager.GroupManager;
import com.tstudy.laoshibang.mode.TGroup;
import com.tstudy.laoshibang.mode.response.GroupDetailResponse;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.SharedPreferencesUtil;
import com.umeng.analytics.MobclickAgent;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
@EActivity(R.layout.splash)
public class SplashActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.updateOnlineConfig(BaseApplication.mContext);
		MobclickAgent.openActivityDurationTrack(false);
//		UpdateExampleConfig.setDeltaUpdate(false);
		PushManager.getInstance().initialize(this.getApplicationContext());
		BaseApplication.checkLogin();
		if (!BaseApplication.checkGroup() ) {
			officialGroup();
		}
	}
	
	@AfterViews
	public void gotoMain(){
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				if (SharedPreferencesUtil.isFirstOpen()) {
					Intent intent = new Intent(SplashActivity.this, GuideActivity_.class);
					SplashActivity.this.startActivity(intent);
					finish();
				}else{
					IndexActivity.show(false,true);
					finish();
				}
			}
		}, 1500);
	}
	
	public void officialGroup(){
		BaseJsonHandler<GroupDetailResponse> response = new BaseJsonHandler<GroupDetailResponse>() {
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, GroupDetailResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					if (response.data != null) {
						response.data.isSelect = 1;
						response.data.userNo = BaseApplication.mUserNo;
						BaseApplication.mCurrentGroup = response.data;
						GroupManager.getInstance().clear(TGroup.class);
						GroupManager.getInstance().insert(response.data);
					}
				}else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected GroupDetailResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return new Gson().fromJson(rawJsonData,GroupDetailResponse.class);
			}
		};
		HttpManager.getInstance().officialGroup(BaseApplication.mUserNo,response);
	}
	
	
	@Override
	public void initArgs() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
